#import "Parser.h"

@implementation Parser

//returns a mutable array full of words in lower case from the string contained in text.
+(NSMutableArray *)arrayOfWordsFromText:(NSString *)text{
	NSString *nextWord;
	NSMutableArray *newWordArray = [[NSMutableArray alloc] init];
	
	//step 1 case
	text = [self makeLowerCase:text];
	
	//Initialises scanner
	NSScanner* myScanner = [NSScanner scannerWithString:text]; 
	NSCharacterSet* whitespaceSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
	
	//scanning
	while(![myScanner isAtEnd]){
		[myScanner scanUpToCharactersFromSet:whitespaceSet intoString:&nextWord]; //get next word
		//add the word to the array
		[newWordArray addObject:nextWord];
	}
	
	return [[newWordArray retain] autorelease];
}

+(NSString*)makeLowerCase:(NSString*)text{
	//NSLog(@"Making Lower Case");
	return [text lowercaseString];
}


+(NSString*)removePunctuation:(NSString*)fromText{
	NSString* tempStr;
//	NSLog(@"Removing Punctuation");
	NSCharacterSet* punctuation = [NSCharacterSet punctuationCharacterSet]; //creates a punctuation character set
	NSCharacterSet* whitespaceSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
	NSScanner* myScanner = [NSScanner scannerWithString:fromText]; //Initialises scanner
	[myScanner setCharactersToBeSkipped:punctuation];
	
	[myScanner scanUpToCharactersFromSet:whitespaceSet intoString:&tempStr];
	return tempStr;
}

+(NSString*)removeHTML:(NSString*)fromText{
	//NSLog(@"Removing HTML");
	return fromText;
}

+(NSMutableArray *)removeDuplicateWordsInArray:(NSArray *)wordArray{
	NSMutableArray *newWordArray = [[NSMutableArray alloc] init];
	NSEnumerator *e = [wordArray objectEnumerator];
	NSString *nextWord;
	//go through the original array, and 
	while(nextWord = [e nextObject]){
		//if the new array does not contain the string in question, insert
		if(![newWordArray containsObject:nextWord]){
			[newWordArray addObject:nextWord];
		}
		//if not, move onto the next one.
	}
	//return the new array
	return [[newWordArray retain] autorelease];
}

@end
